﻿using EntityModel.Extended.Tree;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles.Infrastructure;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog.Config;
using Org.BouncyCastle.Ocsp;
using PddHelper;
using PddHelper.Api;
using PddHelper.Models;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using WebApi.Controllers;
using WebApi.Model;
using WebApiExtend;



namespace WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [Route("system/[Action]")]
    public class systemController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public systemController(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {
        }


        #region 获取用户权限列表

        /// <summary>
        /// 查询:用户权限菜单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Description("查询:用户权限菜单")]
        public ApiResult<List<system_menu>> GetMenus()
        {
            var ret = new ApiResult<List<system_menu>>();
            var list = DbContext<system_menu>().Where(ex => ex.grade > 1 && ex.isdel == 0).OrderBy(ex => ex.levels).ToList();
            ret.Success(list);
            return ret;
        }

        /// <summary>
        /// 查询:用户权限菜单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Description("查询:用户权限菜单")]
        public ApiResult<List<system_menu>> GetMenusByJurisdiction()
        {
            var ret = new ApiResult<List<system_menu>>();
            var MenuIDList = DbContext<system_role_menu_link>().Where(ex => this.UserRoleid.Contains(ex.roleid)).ToList().Select(ex => ex.menuid);
            MenuIDList = DbContext<system_role_menu_link>().Where(ex => ex.isdel == 0).ToList().Select(ex => ex.menuid);
            var list = DbContext<system_menu>().Where(ex => ex.grade > 1 && ex.isdel == 0 && MenuIDList.Contains(ex.id)).OrderBy(ex => ex.levels).ToList();
            ret.Success(list);
            return ret;
        }
        #endregion

        #region 用户__角色

        /// <summary>
        /// 查询:用户权限菜单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Description("查询:用户权限菜单")]
        public ApiResult<dynamic> GetTree()
        {
            var ret = new ApiResult<dynamic>();
            var entity = DbContext<system_menu>().Where(ex => ex.isdel == 0).ToList();
            List<BaseModel.Tree> list = new List<BaseModel.Tree>();

            foreach (var item in entity)
            {
                var arr = item.name.Split(':');
                var result = -1;
                if (item.url == null)
                {
                    item.url = "";
                }
                list.Add(new BaseModel.Tree
                {
                    id = item.id,
                    pid = item.pid,
                    text = result == -1 ? arr[0] : arr[0] + "(" + result + ")",
                    state = item.state,
                    levels = item.levels,
                    //iconCls = item.Icos,
                    attributes = new BaseModel.Attributes()
                    {
                        // target = item.Target,
                        url = ""
                    }
                });
            };

            var firstID = list.OrderBy(ex => ex.levels).FirstOrDefault().id;
            var resultJson = new EntityModel.Extended.Tree.ResultJson();
            resultJson.Results = BaseModel.Tree.GetTree(firstID, list);
            ret.Success(resultJson);
            return ret;
        }


        /// <summary>
        /// 角色_目录
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Description("角色_目录")]
        public ApiResult<dynamic> GetListJurisdictionMenu(system_role par)
        {
            var ret = new ApiResult<dynamic>();
            var listids = DbContext<system_role_menu_link>().Where(ex => ex.roleid == par.id).ToList().Select(ex => ex.menuid);
            var list = DbContext<system_menu>().Where(ex => listids.Contains(ex.id)).OrderBy(ex => ex.grade).ToList();
            ret.Success(list);
            return ret;
        }

        /// <summary>
        /// 添加角色_目录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Description("添加角色_目录")]
        public ApiResult<int> AddJurisdictionMenu(dynamic par)
        {
            var ret = new ApiResult<int>();
            var result = 0;
            int roleid = par.roleid;
            var list = DbContext<system_role_menu_link>().Where(ex => ex.roleid == roleid).ToList();
            foreach (var item in list)
            {
                DbContext<system_role_menu_link>().Delete(item.id);
            }

            foreach (var item in par.MenuList)
            {
                var obj = new system_role_menu_link();
                obj.roleid = par.id;
                obj.menuid = item.id;
                obj.adduserid = this.UserID;

                result = DbContext<system_role_menu_link>().Insert(obj);
            }
            ret.Success(result);
            return ret;
        }


        /// <summary>
        ///获取角色列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost, Description("获取角色列表")]
        public ApiResult<List<system_role>> GetListJurisdictionUserInfoAll()
        {
            var ret = new ApiResult<List<system_role>>();
            var list = DbContext<system_role>().Where(ex => ex.isdel == 0).ToList();

            ret.Success(list);
            return ret;
        }

        /// <summary>
        ///获取用户角色
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost, Description("获取用户角色")]
        public ApiResult<List<system_role>> GetListJurisdictionUserInfo()
        {
            var ret = new ApiResult<List<system_role>>();
            var JurisdictionID = DbContext<system_user_role_link>().Where(ex => ex.userid == this.UserID).ToList().Select(ex => ex.roleid);
            var list = DbContext<system_role>().Where(ex => ex.isdel == 0 && JurisdictionID.Contains(ex.id)).ToList();
            ret.Success(list);
            return ret;
        }

        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Description("添加用户角色")]
        public ApiResult<int> AddJurisdictionUserInfo(dynamic par)
        {
            var ret = new ApiResult<int>();
            var result = 0;
            var list = DbContext<system_user_role_link>().Where(ex => ex.userid == this.UserID).ToList();
            foreach (var item in list)
            {
                DbContext<system_user_role_link>().Delete(item.id);
            }

            if (par.JurisdictionList != null)
            {
                foreach (var item in par.JurisdictionList)
                {
                    var obj = new system_user_role_link();
                    obj.userid = this.UserID;
                    obj.roleid = item.roleid;
                    result = DbContext<system_user_role_link>().Insert(obj);
                }
            }
            ret.Success(result);
            return ret;
        }
        #endregion

    }
}